package com.ukao.cashregister.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.mcxtzhang.commonadapter.rv.OnItemClickListener;
import com.ukao.cashregister.R;
import com.ukao.cashregister.adapter.ServiceTimeAdapter;
import com.ukao.cashregister.adapter.VipGroupListAdapter;
import com.ukao.cashregister.adapter.VipLeverAdapter;
import com.ukao.cashregister.base.BaseDialogFragment;
import com.ukao.cashregister.bean.VipGroupBean;
import com.ukao.cashregister.bean.VipLeverBean;
import com.ukao.cashregister.bean.serviceTimeBean;
import com.ukao.cashregister.consts.GlobalConsts;
import com.ukao.cashregister.eventbus.ChooseTimeEvent;
import com.ukao.cashregister.listener.MyOnItemClickListener;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 选择时间段
 * Created by ukaodev on 2017/6/2.
 */

public class ChooseTimeDialogFragment extends BaseDialogFragment {

    Unbinder unbinder;
    @BindView(R.id.choose_time_recycler)
    RecyclerView mRecycler;
    @BindView(R.id.edit_name_cancel)
    Button mNameCancel;
    @BindView(R.id.edit_name_submit)
    Button mNameSubmit;
    @BindView(R.id.title)
    TextView tvTitle;

    public VipLeverBean.ListBean currentLeverSelect;
    private String mTypeEnter = "";  /*进入类型*/
    private ArrayList<VipLeverBean.ListBean> vipLeverData;
    private ArrayList<VipGroupBean> vipGroupData;
    private VipLeverAdapter mVipLeverAdapter;
    private VipGroupListAdapter mVipGroupListAdapter;
    private VipGroupBean currentGroupSelect;
    private ArrayList<serviceTimeBean.ListBean> mServiceData;
    private ServiceTimeAdapter mServiceTimeAdapter;
    private serviceTimeBean.ListBean currentServiceSelect;
    private String time;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_fragment_choose_time, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            Bundle bundle = getArguments();
            mTypeEnter = bundle.getString("Type");
            switch (mTypeEnter){
                case  GlobalConsts.VIP_LEVER_ENTER :
                    vipLeverData =(ArrayList< VipLeverBean.ListBean>) getArguments().getSerializable("chooseTime");
                    break;
                case   GlobalConsts.VIP_GROUND_ENTER :
                    vipGroupData = (ArrayList< VipGroupBean>)getArguments().getSerializable("chooseTime");
                    break;
                case   GlobalConsts.CREATE_ORDERS_ENTER:
                    mServiceData=(ArrayList<serviceTimeBean.ListBean>)getArguments().getSerializable("chooseTime");
                      time=getArguments().getString("time");
                    break;
            }
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getDialog().setCanceledOnTouchOutside(false);  //点击外面不取消

    }

    private void initView() {
        mRecycler.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        switch (mTypeEnter) {
            case GlobalConsts.VIP_LEVER_ENTER:
                tvTitle.setText("会员卡");
                mVipLeverAdapter=new VipLeverAdapter(activity, vipLeverData);
                mRecycler.setAdapter(mVipLeverAdapter);
                mVipLeverAdapter.setOnItemClickListener(mOnItemClickListener);
                break;
            case GlobalConsts.VIP_GROUND_ENTER:
                tvTitle.setText("分组");
                mVipGroupListAdapter= new VipGroupListAdapter(activity, vipGroupData);
                mRecycler.setAdapter(mVipGroupListAdapter);
                mVipGroupListAdapter.setOnItemClickListener(mOnItemClickListener);
                break;
            case GlobalConsts.CREATE_ORDERS_ENTER:
                tvTitle.setText("取件时间段");
                mServiceTimeAdapter= new ServiceTimeAdapter(activity, mServiceData,time);
                mRecycler.setAdapter(mServiceTimeAdapter);
                mServiceTimeAdapter.setOnItemClickListener(mOnItemClickListener);
                break;
        }
    }
    private OnItemClickListener  mOnItemClickListener=new MyOnItemClickListener() {
        @Override
        public void onItemClick(ViewGroup viewGroup, View view, Object o, int i) {
            super.onItemClick(viewGroup, view, o, i);
            switch (mTypeEnter) {
                case GlobalConsts.VIP_LEVER_ENTER:
                    VipLeverBean.ListBean mitemLever = (VipLeverBean.ListBean) o;
                    if (!mitemLever.isCheck()) {
                        ChooseTimeDialogFragment.this.currentLeverSelect= mitemLever;
                        mitemLever.setCheck(!mitemLever.isCheck());
                        List<VipLeverBean.ListBean>        mData= mVipLeverAdapter.getDatas();
                        for (int j = 0; j < mData.size(); j++) {
                            if (i != j) {
                                mData.get(j).setCheck(false);

                            }
                        }
                        mVipLeverAdapter.notifyDataSetChanged();
                    }
                    break;
                case GlobalConsts.VIP_GROUND_ENTER:
                    VipGroupBean  mItemGroup= (VipGroupBean) o;
                    if (!mItemGroup.isCheck()) {
                        ChooseTimeDialogFragment.this.currentGroupSelect= mItemGroup;
                        mItemGroup.setCheck(!mItemGroup.isCheck());
                        List<VipGroupBean>        mData= mVipGroupListAdapter.getDatas();
                        for (int j = 0; j < mData.size(); j++) {
                            if (i != j) {
                                mData.get(j).setCheck(false);
                            }
                        }

                        mVipGroupListAdapter.notifyDataSetChanged();
                    }


                    break;
                case GlobalConsts.CREATE_ORDERS_ENTER:
                    serviceTimeBean.ListBean mItemService= (serviceTimeBean.ListBean) o;
                    /*&&mItemService.isEnableds()*/
                    if (!mItemService.ischeck()) {
                        ChooseTimeDialogFragment.this.currentServiceSelect= mItemService;
                        mItemService.setIscheck(!mItemService.ischeck());
                        List<serviceTimeBean.ListBean>        mData= mServiceTimeAdapter.getDatas();
                        for (int j = 0; j < mData.size(); j++) {
                            if (i != j) {
                                mData.get(j).setIscheck(false);

                            }
                        }

                        mServiceTimeAdapter.notifyDataSetChanged();
                    }


                    break;
            }

        }
    };
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    protected boolean useEventBus() {
        return true;
    }

    @Subscribe
    public void onEvent(ChooseTimeEvent event) {

    }

    @OnClick({R.id.choose_time_recycler, R.id.edit_name_cancel, R.id.edit_name_submit,R.id.close_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.edit_name_cancel:
                dismiss();
                break;
            case R.id.close_btn:
                dismiss();
                break;
            case R.id.edit_name_submit:
                if (mTypeEnter.equals(GlobalConsts.VIP_LEVER_ENTER)) {
                    ChooseTimeEvent.postHasData(ChooseTimeEvent.Message.lever, currentLeverSelect);
                }else  if (mTypeEnter.equals(GlobalConsts.VIP_GROUND_ENTER)){
                    ChooseTimeEvent.postHasData(ChooseTimeEvent.Message.groud, currentGroupSelect);
                }else{
                    ChooseTimeEvent.postHasData(ChooseTimeEvent.Message.update, currentServiceSelect);
                }
                dismiss();
                break;
        }
    }
}
